//
//  LRXDistributionController.m
//  路人侠
//
//  Created by zhangxulong on 4/3/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXDistributionController.h"
#import "LRXDistributionTableView.h"
#import "AppDelegate.h"
@interface LRXDistributionController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIScrollView * contentScrollView;
@property (nonatomic,weak) UIPageControl * pageView;
@end

@implementation LRXDistributionController
- (NSMutableArray *)selectItemArray
{
    if (_selectItemArray == nil) {
        _selectItemArray = [NSMutableArray array];
    }
    return _selectItemArray;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"正在配送";
    

    
    //设置内容视图
    if (self.selectItemArray.count > 0) {
        [self setContentView];
    }else
    {
        //请求数据
        [self loadSelectItemInfo];
    }
    
}

- (void)loadSelectItemInfo
{
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    [manger POST:getHandledWaiterUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSLog(@"正在加载数据 %@",responseObject);
            if ([responseObject[@"data"][@"status_code"] isEqualToString:@"3"]) {
                
                AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
                appDelegate.userRole = USERNORMALTYPE;
                
                [self popDistributionController];
            }
            else
            {
                for (NSDictionary * modelDic in responseObject[@"data"][@"dataset"]) {
                    LRXHelpMeInfoModel * model = [LRXHelpMeInfoModel helpModelWithDic:modelDic];
                    [self.selectItemArray addObject:model];
                    
                }
                //请求数据
                [self setContentView];
            }
    
        });
     
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}
//设置内容视图
- (void)setContentView{
    UIScrollView * contentScrollView = [[UIScrollView alloc]init];
    self.contentScrollView = contentScrollView;
    contentScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    contentScrollView.contentSize = CGSizeMake(self.selectItemArray.count * self.view.frame.size.width, 0);

    contentScrollView.backgroundColor = [UIColor colorWithRed:211 green:227 blue:257];
    contentScrollView.pagingEnabled = YES;
    contentScrollView.delegate = self;
    
    //添加子SubView
    for (LRXHelpMeInfoModel * seletItemModel in self.selectItemArray) {
        LRXDistributionTableView * tableView = [[LRXDistributionTableView alloc]init];
        tableView.frame = CGRectMake(contentScrollView.frame.size.width * contentScrollView.subviews.count, 0 , contentScrollView.frame.size.width, contentScrollView.frame.size.height);
        [tableView layoutDistributionSubView:seletItemModel];
        [contentScrollView addSubview:tableView];
    }
    
    [self.view addSubview:contentScrollView];
    
    //数量大于1的时候 显示page
    if (self.selectItemArray.count > 1) {
        UIPageControl * pageView = [[UIPageControl alloc]init];
        pageView.center = CGPointMake(self.view.center.x, 407 * hScale);
        pageView.numberOfPages = self.selectItemArray.count;
        self.pageView = pageView;
        [self.view addSubview:pageView];
    }
  
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / self.view.frame.size.width + 0.5;
    self.pageView.currentPage  = page;
}
- (void)popDistributionController
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
